home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWExcLib / FWExcept.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  5.4 KB  |  162 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWExcept.h
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. //========================================================================================
  11. //    THEORY OF OPERATION
  12. //
  13. //    This subsystem provides an emulation for C++ exception handling for compilers that
  14. //    do not yet support exceptions.
  15. //
  16. //    When an exception is thrown, the stack is unwound to the first enclosing try/catch 
  17. //    block.  Automatic (stack) objects are destroyed during stack unwinding.  Exceptions
  18. //    thrown during the construction of either an automatic or a dynamic (heap) object will
  19. //    result in the fully constructed subobjects of the partially constructed object
  20. //    being destroyed.
  21. //
  22. //    To obtain this behavior with exception handling emulation, the programmer must do
  23. //    extra work.  Classes which are to be destroyed during unwinding must be "registered"
  24. //    as auto-destruct classes, and each constructor and destructor must invoke macros
  25. //    so that the emulation subsystem can track which objects have been fully constructed
  26. //    and when they go out of scope.
  27. //
  28. //========================================================================================
  29.  
  30. #ifndef FWEXCEPT_H
  31. #define FWEXCEPT_H
  32.  
  33. #ifndef FW_NATIVE_EXCEPTIONS
  34.  
  35. #ifndef SLPRIMEM_H
  36. #include "SLPriMem.h"
  37. #endif
  38.  
  39. #ifndef FWPRIMEM_H
  40. #include "FWPriMem.h"
  41. #endif
  42.  
  43. #ifndef FWCLAINF_H
  44. #include "FWClaInf.h"
  45. #endif
  46.  
  47. #ifndef FWEXCIMP_H
  48. #include "FWExcImp.h"
  49. #endif
  50.  
  51. #include <stddef.h>
  52. #include <setjmp.h>
  53.  
  54. //========================================================================================
  55. // template <class T> FW_PrivStaticDestroyer(T*)
  56. // 
  57. // A template function that invokes the class T's destructor, using a static dispatch.
  58. // Even if the destructor for class T is virtual, a non-virtual function dispatch will be used.
  59. //========================================================================================
  60.  
  61. template <class T>
  62. inline void FW_PrivStaticDestroyer(T* self)
  63. {
  64.     self->T::~T();
  65. }
  66.  
  67. //========================================================================================
  68. // template <class T> FW_PrivClone(T*)
  69. // 
  70. // A template function that invokes the class T's copy constructor to clone an
  71. // instance of class T into a raw destination buffer
  72. //========================================================================================
  73.  
  74. template <class T>
  75. inline void FW_PrivClone(T* self, void* destination, size_t size)
  76. {
  77. #ifndef FW_DEBUG
  78. FW_UNUSED(size);
  79. #endif
  80.     FW_PRIV_ASSERT(destination);
  81.     FW_PRIV_ASSERT(size >= sizeof(T));
  82.     new(destination) T(*self);
  83. }
  84.  
  85. //========================================================================================
  86. // class FW_CPrivWatcher
  87. //========================================================================================
  88.  
  89. class FW_CPrivWatcher : public FW_SPrivWatcher
  90. {
  91. public:
  92.     FW_CPrivWatcher(FW_PrivDeleteProc deleter)
  93.         { FW_PrivWatcher_Init(this, deleter); }
  94.     ~FW_CPrivWatcher()
  95.         { FW_PrivWatcher_Destroy(this); }
  96. };
  97.     
  98. //========================================================================================
  99. // CLASS FW_CPrivTryBlockContext
  100. //========================================================================================
  101.  
  102. class FW_CPrivTryBlockContext : public FW_SPrivTryBlockContext
  103. {
  104. public:
  105.     FW_CPrivTryBlockContext(jmp_buf buffer, FW_PrivLongJumpProc jumpProc)
  106.         { FW_PrivTryBlockContext_Init(this, buffer, jumpProc); }
  107.  
  108.     ~FW_CPrivTryBlockContext()
  109.         { FW_PrivTryBlockContext_Destroy(this); }
  110. };
  111.  
  112. //========================================================================================
  113. // template <class T> FW_PrivThrowException(const T& exception)
  114. //========================================================================================
  115.  
  116. template <class T>
  117. inline void FW_PrivThrowException(const T& exception)
  118. {
  119.     FW_PrivThrow((void*) &exception, 
  120.             sizeof(T),
  121.             exception.PrivVirtualGetClassInfo(), 
  122.             FW_PrivGetDestroyProc(&exception),
  123.             FW_PrivGetCloneProc(&exception));
  124. }
  125.  
  126. //========================================================================================
  127. // template <class T> FW_PrivGetAutoName(T*)
  128. //
  129. // We declare this function as a template function even though it will "specialized" for every instantiation via
  130. // the FW_DEFINE_AUTO macros.
  131. //========================================================================================
  132.  
  133. #ifdef FW_DEBUG
  134. template <class T>
  135. char* FW_PrivGetAutoName(const T*);
  136. #endif
  137.  
  138. //========================================================================================
  139. // template <class T> FW_PrivGetDestroyProc(T*)
  140. //
  141. // We declare this function as a template function even though it will "specialized" for every instantiation via
  142. // the FW_DEFINE_AUTO macros.
  143. //========================================================================================
  144.  
  145. template <class T>
  146. FW_PrivDestroyProc FW_PrivGetDestroyProc(const T*);
  147.  
  148. //========================================================================================
  149. // template <class T> FW_PrivGetDeleteProc(T*)
  150. //
  151. // We declare this function as a template function even though it will "specialized" for every instantiation via
  152. // the FW_DEFINE_AUTO macros.
  153. //========================================================================================
  154.  
  155. template <class T>
  156. FW_PrivDeleteProc FW_PrivGetDeleteProc(const T*);
  157.  
  158. #endif
  159.  
  160. #endif
  161.  
  162.